<?php

namespace app\common\logic;

class ImageBlur
{

    /**
     * User:Simony
     * Function:
     * @param $srcImg 原图
     * @param null $savepath 保存路径
     * @param null $savename 保存名字
     * @param int $blurFactor 模糊程度
     * @return string
     */
    public function gaussian_blur($srcImg, $savepath = null, $savename = null, $blurFactor = 7)
    {
        $info = getimagesize($srcImg);
        $gdImageResource = null;
        switch ($info[2]) {
            case 1:
                $gdImageResource = imagecreatefromgif($srcImg);
                break;
            case 2:
                $gdImageResource = imagecreatefromjpeg($srcImg);
                break;
            case 3:
                $gdImageResource = imagecreatefrompng($srcImg);
                break;

        }

        $srcImgObj = $this->blur($gdImageResource, $blurFactor);

        $temp = pathinfo($srcImg);
        $name = $temp['basename'];
        $path = $temp['dirname'];
        $exte = $temp['extension'];
        $savename = $savename ? $savename : $name;
        $savepath = $savepath ? $savepath : $path;
        $savefile = $savefile_realpath = $savepath . '/' . $savename;


        $isHttpsOrHttp = preg_match("/^http(s)?:\\/\\/.+/",$savefile_realpath)?true:false;
        if($isHttpsOrHttp){

            $savefile_realpath_info = parse_url($savefile_realpath);
            $savefile =  ROOT_PATH . 'public' . $savefile_realpath_info['path'];
            $savepath_info = parse_url($savepath);
            if(!is_dir( ROOT_PATH . 'public' .$savepath_info['path'])) mkdir(ROOT_PATH . 'public' .$savepath_info['path'], 0777, true);
        }



        $srcinfo = @getimagesize($srcImg);
        switch ($srcinfo[2]) {
            case 1:
                imagegif($srcImgObj, $savefile);
                break;
            case 2:
                imagejpeg($srcImgObj, $savefile);
                break;
            case 3:
                imagepng($srcImgObj, $savefile);
                break;
            default:
                return '保存失败'; //保存失败

        }

        if($isHttpsOrHttp){
            $res = model('common/file/FileLogic', 'logic')->uploadOss(substr($savefile_realpath_info['path'], 1));
        }

      
        return $savefile_realpath;
        imagedestroy($srcImgObj);

    }

    /**、
     * User:Simony
     * Function:
     * @param $gdImageResource 图片资源
     * @param int $blurFactor 可选模糊程度 0使用 3默认 超过5是超级模糊
     * @return mixed
     */

    private function blur($gdImageResource, $blurFactor = 3)

    {


        $blurFactor = round($blurFactor);

        $originalWidth = imagesx($gdImageResource);

        $originalHeight = imagesy($gdImageResource);

        $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));

        $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));


        $prevImage = $gdImageResource;

        $prevWidth = $originalWidth;

        $prevHeight = $originalHeight;


        for ($i = 0; $i < $blurFactor; $i += 1) {


            $nextWidth = $smallestWidth * pow(2, $i);

            $nextHeight = $smallestHeight * pow(2, $i);

            $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);

            imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,

                $nextWidth, $nextHeight, $prevWidth, $prevHeight);


            imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);


            $prevImage = $nextImage;

            $prevWidth = $nextWidth;

            $prevHeight = $nextHeight;

        }


        imagecopyresized($gdImageResource, $nextImage,

            0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);

        imagefilter($gdImageResource, IMG_FILTER_GAUSSIAN_BLUR);
        imagedestroy($prevImage);
        return $gdImageResource;

    }
}

